package edu.ntu.cloudroid.xacml.function;

import java.util.List;


import com.google.appengine.api.datastore.Key;
import com.sun.xacml.EvaluationCtx;
import com.sun.xacml.attr.BooleanAttribute;
import com.sun.xacml.cond.EvaluationResult;
import com.sun.xacml.cond.FunctionBase;

import edu.ntu.cloudroid.model.profile.ProfileData;
import edu.ntu.cloudroid.server.backendservice.FriendService;
import edu.ntu.cloudroid.server.backendservice.ProfileService;
import edu.ntu.cloudroid.xacml.Utilities;
import edu.ntu.cloudroid.xacml.XACMLException;
import edu.ntu.cloudroid.xacml.XACMLStrings;
import edu.ntu.cloudroid.xacml.cache.DataService;

public class SubjectIsFriendOfResource extends FunctionBase
{
   // the parameter types, in order, and whether or not they're bags
   private static final String params[] = {};
   private static final boolean bagParams[] = {};

   public SubjectIsFriendOfResource()
   {
      // use the constructor that handles mixed argument types
      super(XACMLStrings.FN_SubjectIsFriendOfResource, 0, params, bagParams,
            BooleanAttribute.identifier, false);
   }

   @SuppressWarnings("rawtypes")
   @Override
   public EvaluationResult evaluate(List inputs, EvaluationCtx context)
   {	
      try {
         ProfileData subject = ProfileService.getProfile(Utilities.getSubjectPersonKey(context));
         ProfileData resource = ProfileService.getProfile(Utilities.getResourcePersonKey(context));

			boolean evalResult = FriendService.getFriendRelation(
					resource.getEmailAddress(), subject.getEmailAddress()) != null;
			
         //DataService.areFriends(subjectKey, resourceKey);
         return EvaluationResult.getInstance(evalResult);
      } catch (Exception e) {
    	  e.printStackTrace();
         return makeProcessingError("Error in parsing subject ID");
      }
   }
}
